/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package sys.donto.telas;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import sys.donto.consulta.Consulta;
import sys.donto.consulta.ConsultaDAO;
import sys.donto.consulta.ConsultaTableModel;
import sys.donto.dentista.Dentista;
import sys.donto.dentista.DentistaDAO;
import sys.donto.dentista.DentistaTableModel;
import sys.donto.paciente.Paciente;
import sys.donto.paciente.PacienteDAO;
import sys.donto.paciente.PacienteTableModel;
import sys.donto.servico.Servico;
import sys.donto.servico.ServicoDAO;
import sys.donto.servico.ServicoTableModel;
import sys.donto.util.Util;
import java.util.Calendar;
import java.util.Date;

/**
 *
 * @author JOABB
 */
public class telaConsulta extends javax.swing.JDialog {
private Consulta consulta;
    /**
     * Creates new form telaConsulta
     */
    public telaConsulta() { //METODO CONSTRUTOR
       initComponents();
       setModal(true);
       setLocationRelativeTo(null);
       limpaCampos();
       setTitle("sysDONTO - Consultas"); //Define nome no titulo da tela
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel7 = new javax.swing.JPanel();
        btNovo = new javax.swing.JButton();
        btSalvar = new javax.swing.JButton();
        btPesquisar = new javax.swing.JButton();
        btExcluir = new javax.swing.JButton();
        jPanel4 = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        jPanel8 = new javax.swing.JPanel();
        btAjuda = new javax.swing.JButton();
        btSair1 = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        jPanel5 = new javax.swing.JPanel();
        btPesquisarPaciente1 = new javax.swing.JButton();
        btNovoPaciente1 = new javax.swing.JButton();
        tfPaciente = new javax.swing.JTextField();
        jlDentista = new javax.swing.JLabel();
        btPesquisarDentista1 = new javax.swing.JButton();
        btNovoDentista1 = new javax.swing.JButton();
        tfDentista = new javax.swing.JTextField();
        tfHora = new javax.swing.JFormattedTextField();
        jlHora = new javax.swing.JLabel();
        jlServico = new javax.swing.JLabel();
        btPesquisarServico1 = new javax.swing.JButton();
        btNovoServico1 = new javax.swing.JButton();
        tfServico = new javax.swing.JTextField();
        jlData = new javax.swing.JLabel();
        jlPaciente1 = new javax.swing.JLabel();
        tfDataConsulta = new javax.swing.JFormattedTextField();
        Fundo = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setMinimumSize(new java.awt.Dimension(740, 340));
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowOpened(java.awt.event.WindowEvent evt) {
                formWindowOpened(evt);
            }
        });

        jPanel7.setBackground(new java.awt.Color(0, 153, 153));
        jPanel7.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        jPanel7.setPreferredSize(new java.awt.Dimension(750, 50));

        btNovo.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        btNovo.setForeground(new java.awt.Color(255, 255, 255));
        btNovo.setMnemonic('N');
        btNovo.setText("NOVO");
        btNovo.setToolTipText("Novo");
        btNovo.setContentAreaFilled(false);
        btNovo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btNovoActionPerformed(evt);
            }
        });

        btSalvar.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        btSalvar.setForeground(new java.awt.Color(255, 255, 255));
        btSalvar.setMnemonic('S');
        btSalvar.setText("SALVAR");
        btSalvar.setToolTipText("Salvar");
        btSalvar.setContentAreaFilled(false);
        btSalvar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btSalvarActionPerformed(evt);
            }
        });

        btPesquisar.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        btPesquisar.setForeground(new java.awt.Color(255, 255, 255));
        btPesquisar.setMnemonic('P');
        btPesquisar.setText("PESQUISAR");
        btPesquisar.setToolTipText("Pesquisar");
        btPesquisar.setContentAreaFilled(false);
        btPesquisar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btPesquisarActionPerformed(evt);
            }
        });

        btExcluir.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        btExcluir.setForeground(new java.awt.Color(255, 255, 255));
        btExcluir.setMnemonic('E');
        btExcluir.setText("EXCLUIR");
        btExcluir.setToolTipText("Excluir");
        btExcluir.setContentAreaFilled(false);
        btExcluir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btExcluirActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel7Layout = new javax.swing.GroupLayout(jPanel7);
        jPanel7.setLayout(jPanel7Layout);
        jPanel7Layout.setHorizontalGroup(
            jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel7Layout.createSequentialGroup()
                .addContainerGap(370, Short.MAX_VALUE)
                .addComponent(btNovo)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btSalvar)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btPesquisar)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btExcluir)
                .addContainerGap())
        );
        jPanel7Layout.setVerticalGroup(
            jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel7Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btSalvar)
                    .addComponent(btNovo)
                    .addComponent(btPesquisar)
                    .addComponent(btExcluir))
                .addContainerGap(12, Short.MAX_VALUE))
        );

        jPanel4.setBackground(new java.awt.Color(153, 153, 153));

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel3.setForeground(new java.awt.Color(255, 255, 255));
        jLabel3.setText("sysDONTO ® - Sistema de Odontologia");

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel4Layout.createSequentialGroup()
                .addContainerGap(515, Short.MAX_VALUE)
                .addComponent(jLabel3)
                .addContainerGap())
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel4Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jLabel3)
                .addGap(23, 23, 23))
        );

        jPanel8.setBackground(new java.awt.Color(0, 153, 153));
        jPanel8.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        jPanel8.setPreferredSize(new java.awt.Dimension(750, 50));

        btAjuda.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        btAjuda.setForeground(new java.awt.Color(255, 255, 255));
        btAjuda.setIcon(new javax.swing.ImageIcon(getClass().getResource("/sys/donto/imagens/pp-help.png"))); // NOI18N
        btAjuda.setMnemonic('A');
        btAjuda.setText("Ajuda");
        btAjuda.setToolTipText("Ajuda");
        btAjuda.setContentAreaFilled(false);
        btAjuda.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btAjudaActionPerformed(evt);
            }
        });

        btSair1.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        btSair1.setForeground(new java.awt.Color(255, 255, 255));
        btSair1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/sys/donto/imagens/cancel-button-pp.png"))); // NOI18N
        btSair1.setMnemonic('S');
        btSair1.setText("Sair");
        btSair1.setToolTipText("Sair");
        btSair1.setContentAreaFilled(false);
        btSair1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btSair1ActionPerformed(evt);
            }
        });

        jLabel4.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jLabel4.setForeground(new java.awt.Color(255, 255, 255));
        jLabel4.setText("Consultas");

        javax.swing.GroupLayout jPanel8Layout = new javax.swing.GroupLayout(jPanel8);
        jPanel8.setLayout(jPanel8Layout);
        jPanel8Layout.setHorizontalGroup(
            jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel8Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 401, Short.MAX_VALUE)
                .addComponent(btAjuda)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btSair1)
                .addGap(23, 23, 23))
        );
        jPanel8Layout.setVerticalGroup(
            jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel8Layout.createSequentialGroup()
                .addGroup(jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btAjuda)
                    .addComponent(btSair1))
                .addGap(0, 0, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel8Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jLabel4)
                .addContainerGap())
        );

        jPanel5.setBackground(new java.awt.Color(255, 255, 255));
        jPanel5.setLayout(null);

        btPesquisarPaciente1.setBackground(new java.awt.Color(0, 153, 153));
        btPesquisarPaciente1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/sys/donto/imagens/Search 16.png"))); // NOI18N
        btPesquisarPaciente1.setToolTipText("Pesquisar Paciente");
        btPesquisarPaciente1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btPesquisarPaciente1ActionPerformed(evt);
            }
        });
        jPanel5.add(btPesquisarPaciente1);
        btPesquisarPaciente1.setBounds(610, 100, 37, 25);

        btNovoPaciente1.setBackground(new java.awt.Color(0, 153, 153));
        btNovoPaciente1.setForeground(new java.awt.Color(0, 153, 153));
        btNovoPaciente1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/sys/donto/imagens/add-16.png"))); // NOI18N
        btNovoPaciente1.setToolTipText("Novo Paciente");
        btNovoPaciente1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btNovoPaciente1ActionPerformed(evt);
            }
        });
        jPanel5.add(btNovoPaciente1);
        btNovoPaciente1.setBounds(650, 100, 37, 25);

        tfPaciente.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
        tfPaciente.setEnabled(false);
        tfPaciente.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tfPacienteActionPerformed(evt);
            }
        });
        jPanel5.add(tfPaciente);
        tfPaciente.setBounds(80, 100, 520, 20);

        jlDentista.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jlDentista.setText("Dentista:");
        jPanel5.add(jlDentista);
        jlDentista.setBounds(20, 130, 56, 15);

        btPesquisarDentista1.setBackground(new java.awt.Color(0, 153, 153));
        btPesquisarDentista1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/sys/donto/imagens/Search 16.png"))); // NOI18N
        btPesquisarDentista1.setToolTipText(" Pesquisar Dentista");
        btPesquisarDentista1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btPesquisarDentista1ActionPerformed(evt);
            }
        });
        jPanel5.add(btPesquisarDentista1);
        btPesquisarDentista1.setBounds(610, 130, 37, 25);

        btNovoDentista1.setBackground(new java.awt.Color(0, 153, 153));
        btNovoDentista1.setForeground(new java.awt.Color(0, 153, 153));
        btNovoDentista1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/sys/donto/imagens/add-16.png"))); // NOI18N
        btNovoDentista1.setToolTipText("Novo Dentista");
        btNovoDentista1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btNovoDentista1ActionPerformed(evt);
            }
        });
        jPanel5.add(btNovoDentista1);
        btNovoDentista1.setBounds(650, 130, 37, 25);

        tfDentista.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
        tfDentista.setEnabled(false);
        tfDentista.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tfDentistaActionPerformed(evt);
            }
        });
        jPanel5.add(tfDentista);
        tfDentista.setBounds(80, 130, 520, 20);

        tfHora.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
        try {
            tfHora.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("##:##")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }
        tfHora.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tfHoraActionPerformed(evt);
            }
        });
        tfHora.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                tfHoraFocusGained(evt);
            }
        });
        jPanel5.add(tfHora);
        tfHora.setBounds(250, 40, 50, 20);

        jlHora.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jlHora.setText("Hora:");
        jPanel5.add(jlHora);
        jlHora.setBounds(210, 40, 33, 15);

        jlServico.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jlServico.setText("Serviço:");
        jPanel5.add(jlServico);
        jlServico.setBounds(20, 70, 48, 15);

        btPesquisarServico1.setBackground(new java.awt.Color(0, 153, 153));
        btPesquisarServico1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/sys/donto/imagens/Search 16.png"))); // NOI18N
        btPesquisarServico1.setToolTipText("Pesquisar Serviço");
        btPesquisarServico1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btPesquisarServico1ActionPerformed(evt);
            }
        });
        jPanel5.add(btPesquisarServico1);
        btPesquisarServico1.setBounds(610, 70, 37, 25);

        btNovoServico1.setBackground(new java.awt.Color(0, 153, 153));
        btNovoServico1.setForeground(new java.awt.Color(0, 153, 153));
        btNovoServico1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/sys/donto/imagens/add-16.png"))); // NOI18N
        btNovoServico1.setToolTipText("Novo Serviço");
        btNovoServico1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btNovoServico1ActionPerformed(evt);
            }
        });
        jPanel5.add(btNovoServico1);
        btNovoServico1.setBounds(650, 70, 37, 25);

        tfServico.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
        tfServico.setEnabled(false);
        tfServico.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tfServicoActionPerformed(evt);
            }
        });
        jPanel5.add(tfServico);
        tfServico.setBounds(80, 70, 520, 20);

        jlData.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jlData.setText("Data*:");
        jPanel5.add(jlData);
        jlData.setBounds(30, 40, 50, 20);

        jlPaciente1.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jlPaciente1.setText("Paciente:");
        jPanel5.add(jlPaciente1);
        jlPaciente1.setBounds(20, 100, 56, 15);

        tfDataConsulta.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
        try {
            tfDataConsulta.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("##/##/####")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }
        tfDataConsulta.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                tfDataConsultaFocusLost(evt);
            }
        });
        jPanel5.add(tfDataConsulta);
        tfDataConsulta.setBounds(80, 40, 110, 20);

        Fundo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/sys/donto/imagens/fundos/fundo_cidade.jpg"))); // NOI18N
        jPanel5.add(Fundo);
        Fundo.setBounds(0, 0, 740, 200);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jPanel8, javax.swing.GroupLayout.DEFAULT_SIZE, 740, Short.MAX_VALUE)
            .addComponent(jPanel7, javax.swing.GroupLayout.DEFAULT_SIZE, 740, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, 0)
                .addComponent(jPanel8, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel5, javax.swing.GroupLayout.PREFERRED_SIZE, 200, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel7, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
 private void limpaCampos(){
        tfDataConsulta.setText("");
        tfDentista.setText("");
        tfHora.setText("");
        tfPaciente.setText("");
        tfServico.setText("");
        btExcluir.setEnabled(false);
        consulta = new Consulta();
        
        
    }

    private void btPesquisarPaciente1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btPesquisarPaciente1ActionPerformed
        PacienteDAO dao = new PacienteDAO();// butão de pesquisa de estado.
        List<Paciente> lista = dao.listar();
       PacienteTableModel ptm = new PacienteTableModel(lista);
        Object o = telaPesquisa.exibeTela(ptm, "paciente");
        Paciente p;
        if (o != null) {
            p = dao.pesquisaId(Integer.valueOf(String.valueOf(o)));
            consulta.setPaciente(p);
            tfPaciente.setText(p.getNome());
        }         // TODO add your handling code here:
    }//GEN-LAST:event_btPesquisarPaciente1ActionPerformed

    private void btNovoPaciente1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btNovoPaciente1ActionPerformed
         telaPaciente tp = new telaPaciente();
        tp.setVisible(true);
    }//GEN-LAST:event_btNovoPaciente1ActionPerformed

    private void tfPacienteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tfPacienteActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_tfPacienteActionPerformed

    private void btPesquisarDentista1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btPesquisarDentista1ActionPerformed
          DentistaDAO dao = new DentistaDAO();// butão de pesquisa de estado.
        List<Dentista> lista = dao.listar();
       DentistaTableModel dtm = new DentistaTableModel(lista);
        Object o = telaPesquisa.exibeTela(dtm, "dentista");
        Dentista d;
        if (o != null) {
            d = dao.pesquisaId(Integer.valueOf(String.valueOf(o)));
            consulta.setDentista(d);
            tfDentista.setText(d.getNome());      // TODO add your handling code here:
    }//GEN-LAST:event_btPesquisarDentista1ActionPerformed
    }
    private void tfDentistaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tfDentistaActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_tfDentistaActionPerformed

    private void tfHoraActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tfHoraActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_tfHoraActionPerformed

    private void btPesquisarServico1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btPesquisarServico1ActionPerformed
        ServicoDAO dao = new ServicoDAO();// butão de pesquisa de estado.
        List<Servico> lista = dao.listar();
        ServicoTableModel stm = new ServicoTableModel(lista);
        Object o = telaPesquisa.exibeTela(stm, "servico");
        Servico s;
        if (o != null) {
            s = dao.pesquisaId(Integer.valueOf(String.valueOf(o)));
            consulta.setServico(s);
            tfServico.setText(s.getDescricao());
        }        // TODO add your handling code here:
    }//GEN-LAST:event_btPesquisarServico1ActionPerformed

    private void btNovoServico1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btNovoServico1ActionPerformed
         telaServico ts = new telaServico();
        ts.setVisible(true);
    }//GEN-LAST:event_btNovoServico1ActionPerformed

    private void tfServicoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tfServicoActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_tfServicoActionPerformed

    private void formWindowOpened(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowOpened
        
    }//GEN-LAST:event_formWindowOpened

    private void btNovoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btNovoActionPerformed
        limpaCampos(); //CHAMA METODO PARA LIMPAR CAMPOS
    }//GEN-LAST:event_btNovoActionPerformed

    private void btSalvarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btSalvarActionPerformed
        if (Util.chkVazio(tfDentista.getText(), tfHora.getText(),tfPaciente.getText(),
                tfServico.getText(), tfDataConsulta.getText())){
//Condição para verificação se campos estão vzios.
            
            consulta.setHora(tfHora.getText());
            //consulta.setDataConsulta(jdCalendario.getCalendar().getTime());

           //METODO NECESSÁRIO PARA SALVAR A DATA NO BANCO DE DADOS
            try {
                if (!tfDataConsulta.getText().replaceAll("/", "").isEmpty()) {
                    DateFormat fmt = new SimpleDateFormat("dd/MM/yyyy");
                    java.util.Date data1;
                    data1 = new java.util.Date(fmt.parse(tfDataConsulta.getText()).getTime());
                    consulta.setDataConsulta(data1);
                }
            } catch (ParseException ex) {
                Logger.getLogger(telaConsulta.class.getName()).log(Level.SEVERE, null, ex);

            }
            
            ConsultaDAO dao = new ConsultaDAO();
            dao.salvar(consulta);// chama o metodo salvar da classe cidade.
            limpaCampos();

            JOptionPane.showMessageDialog(rootPane, "Consulta agendada com sucesso!");
        }
    }//GEN-LAST:event_btSalvarActionPerformed

    private void btPesquisarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btPesquisarActionPerformed
        limpaCampos();
        ConsultaDAO dao = new ConsultaDAO();
        List <Consulta> consultas = (!tfPaciente.getText().isEmpty() ? dao.pesquisaDescricao(tfPaciente.getText())
            : dao.listar()); //descrição que diz que se o campo descrição estiver vazio ele pega a descrição digitada.

        ConsultaTableModel ctm = new ConsultaTableModel(consultas);

        Object o = telaPesquisa.exibeTela(ctm, "Consulta");

        if (o != null){
            consulta = new Consulta();
            consulta = dao.pesquisaId(Integer.valueOf(String.valueOf(o)));

//            tfDataConsulta.setValue(consulta.getDataConsulta());
            SimpleDateFormat sf = new SimpleDateFormat("dd/MM/yyyy");
            tfDataConsulta.setText(sf.format(consulta.getDataConsulta()));
            
            tfHora.setText(consulta.getHora());
            tfDentista.setText(consulta.getDentista().getNome());
            tfPaciente.setText(consulta.getPaciente().getNome());
            tfServico.setText(consulta.getServico().getDescricao());
            
          
            btExcluir.setEnabled(true);
        }
    }//GEN-LAST:event_btPesquisarActionPerformed

    private void btExcluirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btExcluirActionPerformed
         if (JOptionPane.showConfirmDialog(rootPane,
                    "Deseja desmarcar Consulta?",
                    "Desmarcar?", JOptionPane.YES_NO_OPTION)
                    == JOptionPane.YES_OPTION) {
        ConsultaDAO dao = new ConsultaDAO();
        dao.remover(consulta);//metodopara remover, excluir.
        limpaCampos();

        JOptionPane.showMessageDialog(rootPane, "Consulta desmarcada com Sucesso!");
        }else{
             limpaCampos();
         }
        
    }//GEN-LAST:event_btExcluirActionPerformed

    private void btAjudaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btAjudaActionPerformed
        telaAjuda ta = new telaAjuda();
        ta.setVisible(true);        // TODO add your handling code here:
    }//GEN-LAST:event_btAjudaActionPerformed

    private void btSair1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btSair1ActionPerformed
        dispose();
    }//GEN-LAST:event_btSair1ActionPerformed

    private void tfHoraFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_tfHoraFocusGained
        
    }//GEN-LAST:event_tfHoraFocusGained

    private void btNovoDentista1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btNovoDentista1ActionPerformed
        telaDentista td = new telaDentista();
        td.setVisible(true);        // TODO add your handling code here:
    }//GEN-LAST:event_btNovoDentista1ActionPerformed

    private void tfDataConsultaFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_tfDataConsultaFocusLost
        if(Util.dataValida(tfDataConsulta.getText().toString())== false){
         JOptionPane.showMessageDialog(rootPane, "Data Inválida, redigite!", "ERRO", JOptionPane.ERROR_MESSAGE);
         tfDataConsulta.setText("");
     }
    
    }//GEN-LAST:event_tfDataConsultaFocusLost

   
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(telaConsulta.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(telaConsulta.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(telaConsulta.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(telaConsulta.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new telaConsulta().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel Fundo;
    private javax.swing.JButton btAjuda;
    private javax.swing.JButton btExcluir;
    private javax.swing.JButton btNovo;
    private javax.swing.JButton btNovoDentista1;
    private javax.swing.JButton btNovoPaciente1;
    private javax.swing.JButton btNovoServico1;
    private javax.swing.JButton btPesquisar;
    private javax.swing.JButton btPesquisarDentista1;
    private javax.swing.JButton btPesquisarPaciente1;
    private javax.swing.JButton btPesquisarServico1;
    private javax.swing.JButton btSair1;
    private javax.swing.JButton btSalvar;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPanel jPanel7;
    private javax.swing.JPanel jPanel8;
    private javax.swing.JLabel jlData;
    private javax.swing.JLabel jlDentista;
    private javax.swing.JLabel jlHora;
    private javax.swing.JLabel jlPaciente1;
    private javax.swing.JLabel jlServico;
    private javax.swing.JFormattedTextField tfDataConsulta;
    private javax.swing.JTextField tfDentista;
    private javax.swing.JFormattedTextField tfHora;
    private javax.swing.JTextField tfPaciente;
    private javax.swing.JTextField tfServico;
    // End of variables declaration//GEN-END:variables

    

}
